package com.smm.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class SHA1Util {
    private static Logger log = LoggerFactory.getLogger(SHA1Util.class);

    public static String sha1(String data) {
        String result = null;
        data += "lhq";
        try {
            MessageDigest md = MessageDigest.getInstance("SHA1");
            byte[] b = data.getBytes();

            md.update(b);

            byte[] b2 = md.digest();
            int len = b2.length;
            String str = "0123456789abcdef";
            char[] ch = str.toCharArray();
            char[] chars = new char[len * 2];

            for (int i = 0, k = 0; i < len; i++) {
                byte b3 = b2[i];
                chars[k++] = ch[b3 >>> 4 & 0xf];
                chars[k++] = ch[b3 & 0xf];
            }
            result = new String(chars);
        } catch (NoSuchAlgorithmException e) {
            log.error("sha1加密错误", e);
        }


        return result;
    }


    public static boolean validate(String unknown, String sha1String) {
        return sha1String.equals(sha1(unknown));
    }

}

